<template>
  <div class="type-nav">
    <div class="container">
      <div
        @mouseenter="categoryListMouseEnter"
        @mouseleave="categoryListMouseLeave"
      >
        <h2 class="all">全部商品分类</h2>
        <transition name="categoryList">
          <div
            class="sort"
            @mouseleave="currentIndex = -1"
            v-show="categoryListIsShow"
          >
            <div class="all-sort-list2">
              <div
                class="item"
                v-for="(
                  levelOneCategory, levelOneCategoryIndex
                ) in categoryList"
                :key="levelOneCategory.categoryId"
                :class="{ item_on: currentIndex === levelOneCategoryIndex }"
                @mouseenter="enterThrottle(levelOneCategoryIndex)"
                @click="toSearch"
              >
                <h3>
                  <a
                    href="javascript:;"
                    :data-categoryName="levelOneCategory.categoryName"
                    :data-category1Id="levelOneCategory.categoryId"
                    >{{ levelOneCategory.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="levelTwoCategory in levelOneCategory.categoryChild"
                      :key="levelTwoCategory.categoryId"
                    >
                      <dt>
                        <a
                          href="javascript:;"
                          :data-categoryName="levelTwoCategory.categoryName"
                          :data-category2Id="levelTwoCategory.categoryId"
                          >{{ levelTwoCategory.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="levelThreeCategory in levelTwoCategory.categoryChild"
                          :key="levelThreeCategory.categoryId"
                        >
                          <a
                            href="javascript:;"
                            :data-categoryName="levelThreeCategory.categoryName"
                            :data-category3Id="levelThreeCategory.categoryId"
                            >{{ levelThreeCategory.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import _ from "lodash";
import { mapState } from "vuex";
export default {
  name: "NavType",
  data() {
    return {
      currentIndex: -1,
      categoryListIsShow: false,
    };
  },
  computed: {
    ...mapState("NavType", ["categoryList"]),
  },
  methods: {
    // 优化节流(三级列表的数据是一级一级请求过来的，可以减少请求次数)(所有的 item 必须使用同一个节流函数)
    enterThrottle: _.throttle(
      function (index) {
        this.currentIndex = index;
      },
      35,
      { leading: true, trailing: true }
    ),
    // 三级分类列表路由跳转事件(事件委托)
    toSearch(e) {
      const el = e.target;
      // 判断是否为期望元素
      if (el.nodeName.toLowerCase() === "a") {
        // dataset 中的属性会转成小写
        const { categoryname, category1id, category2id, category3id } =
          el.dataset;
        // 收集路由参数
        const query = {};
        query.categoryName = categoryname;
        if (category1id) {
          query.category1Id = category1id;
        } else if (category2id) {
          query.category2Id = category2id;
        } else if (category3id) {
          query.category3Id = category3id;
        }
        // 编程式路由导航(声明式路由导航 router-link 会 转化为 a 标签，当数量过多时会造成性能问题)
        const location = {
          name: "search",
          // 如果没有 params 则解析路由的时候 params.keywords 为 undefined 没有问题
          params: this.$route.params,
          query,
        }
        this.$router.push(location);
      }
    },
    // 三级分类列表鼠标移入事件
    categoryListMouseEnter() {
      this.categoryListIsShow = true;
    },
    // 三级分类列表鼠标移出事件
    categoryListMouseLeave() {
      // 如果当前路由为 home 鼠标移出 categoryList 不隐藏
      if (this.$route.path.includes("home")) return;
      this.categoryListIsShow = false;
    },
  },
  watch: {
    // 点击 categoryList 时路径后的参数改变，隐藏 categoryList
    "$route.fullPath": {
      handler() {
        this.categoryListIsShow = false;
      },
    },
  },
  mounted() {
    // 初始加载路由时判断当前是否为 home，如果是就显示 categoryList
    if (this.$route.path.includes("home")) {
      this.categoryListMouseEnter();
    }
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &.item_on {
            .item-list {
              display: block;
            }
          }
        }
      }
    }

    & .categoryList-enter,
    & .categoryList-leave-to {
      opacity: 0;
    }

    & .categoryList-enter-active,
    & .categoryList-leave-active {
      transition: opacity 0.3s linear;
    }
  }
}
</style>